const gulp = require('gulp');
const htmlmin = require('gulp-htmlmin');    // html压缩
const babel = require('gulp-babel');        // js编译
const uglify = require('gulp-uglify');      // js压缩

const sass = require('gulp-sass-china');    // scss编译
sass.compiler = require('sass');

const cleanCSS = require('gulp-clean-css'); // css压缩
const fileinclude = require('gulp-file-include');   // html拼接工具



// 1.html
gulp.task('html',async()=>{
    gulp.src(['./src/html/**/*.html','!./src/html/template/**/*.html'])
    .pipe(fileinclude({
        prefix: '@@',
        basepath: '@file'
    }))
    .pipe(htmlmin({ collapseWhitespace: true }))
    .pipe(gulp.dest('./dist'))
})

// 2.js
gulp.task('js',async()=>{
    gulp.src(['./src/javascripts/**/*.js','!src/javascripts/libs/**/*.js'])
    .pipe(babel({
        presets:['@babel/env']
    }))
    .pipe(uglify())
    .pipe(gulp.dest('./dist/javascripts'))
})

// 3.scss
gulp.task('scss',async()=>{
    gulp.src(['./src/scss/**/*.scss','!./src/scss/libs/**/*.scss'])
    .pipe(sass().on('error', sass.logError))
    .pipe(cleanCSS())
    .pipe(gulp.dest('./dist/css'))
})

// 4.libs：不进行代码更新的js、scss文件单独提取出来
gulp.task('libs',async()=>{
    // js
    gulp.src(['./src/javascripts/libs/**/*.js'])
    .pipe(uglify())
    .pipe(gulp.dest('./dist/javascripts/libs'));

    // scss
    gulp.src(['./src/scss/libs/**/*.scss'])
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest('./dist/css/libs'));
})


gulp.task('build',gulp.series('libs','html','js','scss'));


